package com.neusoft.elmboot.service.impl;

import java.util.ArrayList;

import java.util.List;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.elmboot.mapper.BusinessMapper;
import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.po.Comment;
import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.BusinessService;
import com.neusoft.elmboot.util.AESUtils;
import com.neusoft.elmboot.util.TokenUtils;

@Service
public class BusinessServiceImpl implements BusinessService{
	String key  = "woniucsdnvip8888";
	
	@Autowired
	private BusinessMapper businessMapper;

	@Override
	public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) {
		return businessMapper.listBusinessByOrderTypeId(orderTypeId);
	}
	
	@Override
	public Business getBusinessById(Integer businessId) {
		return businessMapper.getBusinessById(businessId);
	}
	
	@Override
	public List<Business> listBusinessByWords(String words){
		List<Term> terms  = StandardTokenizer.segment(words);
		List<String> wordList = new ArrayList<>();
        for (Term term : terms) {
        	wordList.add(term.word); // 将每个 Term 对象的 word 属性添加到列表中
	    }
	    return businessMapper.listBusinessByWords(wordList);
	}
	
	
	@Override
	public Business getBusinessByIdByPass(Business business) {
		Business b=businessMapper.getBusinessByIdByPass(business);
		if(b!=null) {
			String token = TokenUtils.getToken(b.getBusinessPassId().toString(), b.getPassword(),"business");
			token=AESUtils.encrypt(token, key);
			b.setToken(token);
			b.setPrivateKey("");
		}
		return b;
	}
	
	@Override
	public int getBusinessByPassId(String businessPassId) {
		return businessMapper.getBusinessByPassId(businessPassId);
	}
	
	@Override
	public int saveBusiness(Business business) {
		return businessMapper.saveBusiness(business);
	}
	
	
	@Override
	public int saveKey(Business business){
		int flag=businessMapper.saveKey(business);
		return flag;
	}
	
	@Override
	public String getPrivateKey(String businessPassId) {
		return businessMapper.getPrivateKey(businessPassId);
	}
	
	@Override
	public String getPublicKey(String businessPassId) {
		return businessMapper.getPublicKey(businessPassId);
	}
	
	@Override
	public int updateBusiness(Business business) {
		return businessMapper.updateBusiness(business);
	}
	
	@Override
	public Business getEntireBusinessById(String businessPassId) {
		Business b=businessMapper.getEntireBusinessById(businessPassId);
		if(b!=null) {
			b.setPrivateKey("");
		}
		return  b;
	}
	
	
}
